<template>
    <div class="app-container">

      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="施工名称" prop="constructionName">
          <el-input
            v-model="queryParams.constructionName"
            placeholder="请输入施工名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="施工状态" prop="publishStatus">
          <el-select v-model="queryParams.publishStatus" placeholder="请选择人员状态" clearable size="small">
            <el-option label="下架" value="0"/>
            <el-option label="上架" value="1"/>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="queryParams.auditStatus" placeholder="请选择人员审核状态" clearable size="small">
            <el-option label="未审核" value="0"/>
            <el-option label="已审核" value="1"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-card>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['construction:info:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['construction:info:update']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['construction:info:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['construction:info:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="infoList" border @selection-change="handleSelectionChange" @row-dblclick="getPicAndVideo">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="施工ID" align="center" prop="constructionId" />
          <el-table-column label="施工名称" align="center" prop="constructionName" :show-overflow-tooltip="true" />
<!--          <el-table-column label="供应商" align="center" prop="supplierInfo.supplierName" :show-overflow-tooltip="true" />-->
          <el-table-column label="类别信息" align="center" prop="team.teamName" :show-overflow-tooltip="true" />
          <el-table-column label="价格" align="center" prop="price" :show-overflow-tooltip="true" />
          <el-table-column label="队伍人数" align="center" prop="teamNumber" :show-overflow-tooltip="true" />
          <el-table-column label="施工类型" align="center" prop="constructionType" :show-overflow-tooltip="true" >
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.constructionType === '0' ? 'success' : ''"
                disable-transitions>{{scope.row.constructionType ==='0' ? '建房施工' : scope.row.constructionType ==='6' ? '同城安装' : '装修施工'}}</el-tag>
            </template>
          </el-table-column>
<!--          scope.row.constructionType ==='0' ? '建房施工' : '装修施工'-->
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.publishStatus" active-value="0" inactive-value="1" @change="handlePublishStatusChange(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="审核状态" align="center">
            <template slot-scope="scope">
              <el-switch v-if="scope.row.auditStatus == 1" disabled="disabled" v-model="scope.row.auditStatus" active-value="1" inactive-value="0"></el-switch>
              <el-switch v-else-if="scope.row.auditStatus == 0" v-model="scope.row.auditStatus" @change="handleAuditStatusChange(scope.row)" active-value="1" inactive-value="0"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="描述" align="center" prop="descript" :show-overflow-tooltip="true" />
          <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['construction:info:update']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['construction:info:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-card>

      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="施工名称" prop="constructionName">
                <el-input v-model="form.constructionName" placeholder="请输入人员姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="价格" prop="price">
                <el-input v-model="form.price" placeholder="请输入价格" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="13">
              <el-form-item label="施工规模" prop="teamNumber">
                  <el-input v-model="form.teamNumber" placeholder="请输入人员规模" />
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="上架状态" prop="publishStatus">
                <el-switch v-model="form.publishStatus" active-value="1" inactive-value="0"></el-switch>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="施工描述" prop="descript">
                <el-input v-model="form.descript" type="textarea" :autosize="{ minRows: 2, maxRows: 6}" placeholder="请输入施工队人员描述" />
              </el-form-item>
            </el-col>
          </el-row>

<!--          <el-row>-->
<!--            <el-col :span="24">-->
<!--              <el-form-item label="施工队类型" prop="constructionType">-->
<!--                &lt;!&ndash;<el-input v-model="form.descript" type="textarea" :autosize="{ minRows: 2, maxRows: 6}" placeholder="请输入施工队人员描述" />&ndash;&gt;-->
<!--                <el-select  v-model="form.constructionType">-->
<!--                  <el-option label="建房施工" value="0" />-->
<!--                  <el-option label="装修施工" value="1" />-->
<!--                  <el-option label="同城安装" value="6" />-->
<!--                </el-select>-->
<!--              </el-form-item>-->
<!--            </el-col>-->
<!--          </el-row>-->


        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

    </div>
</template>

<script>

  import { listInfo,getInfo,addInfo,updateInfo,deleteInfo,changeInfoStatus,changeInfoAuditStatus,exportInfo } from "@/api/construction/info";


  export default {
    name: "Info",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户表格数据
        infoList: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 状态数据字典
        statusOptions: [],
        // 类别选项
        teamOptions: [],
        // 供应商选项
        supplierOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          constructionCore: null,
          constructionName: null,
          publishStatus: null,
          auditStatus: null,
          supplierId: null,
          teamId: null,
        },
        // 类别ID
        teamId: null,
        // 商家ID
        supplierId: null,
        // 表单参数
        form: {},
        defaultProps: {
          children: "children",
          label: "label",
        },
        // 表单校验
        rules: {
          constructionName: [
            { required: true, message: "人员名称不能为空", trigger: "blur" },
          ],
          price: [
            {required:true, message: "价格不能为空", trigger : "blur"},
          ],
          descript: [
            {required:true, message: "人员描述不能为空", trigger : "blur"},
          ],
          teamNumber: [
            {required:true, message: "人员规模不能为空", trigger : "blur"},
          ],
        },
      };
    },
    created() {
      let supplierId = parseInt(this.$route.query.supplierId);
      let teamId = parseInt(this.$route.query.teamId);

      this.supplierId = supplierId;
      this.teamId = teamId;
      this.getList(supplierId,teamId);
      this.getDicts("sys_normal_disable").then((response) => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      /* 多选框选中数据 **/
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.infoId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },

      /** 查询施工队图片和视频列表 */
      getPicAndVideo(row){
        this.$router.push({path:"/picAndVideo/picVideo",query:{constructionId:row.constructionId}});
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList(this.supplierId,this.teamId);
      },

      /** 重置 */
      resetQuery(){
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /* 查询类别列表 **/
      getList(supplierId,teamId){
        this.loading = true;
        this.queryParams.supplierId = supplierId;
        this.queryParams.teamId = teamId;
        listInfo(this.queryParams).then(response => {
          this.infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      /* 取消按钮 **/
      cancel() {
        this.open = false;
        this.reset();
      },

      /* 表单重置 **/
      reset() {
        this.form = {
          constructionId: undefined,
          constructionCore: undefined,
          constructionName: undefined,
          supplierId: undefined,
          teamId: undefined,
          teamNumber:undefined,
          publishStatus: "0",
          auditStatus: "0",
          price: undefined,
          descript: undefined,
        };
        this.resetForm("form");
      },

      /* 查询 **/
      handleAdd(){
        this.reset();
        getInfo().then((response) => {
          this.supplierOptions = response.suppliers;
          this.teamOptions = response.teams;
          this.open = true;
          this.title = "添加人员";
        });
      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const constructionId = row.constructionId || this.ids;
        getInfo(constructionId).then((response) => {
          this.form = response.data;
          this.supplierOptions = response.suppliers;
          this.teamOptions = response.teams;
          this.form.supplierId = response.supplierId;
          this.form.teamId = response.teamId;
          this.open = true;
          this.title = "修改人员";
        });
      },

      /** 删除 */
      handleDelete(row){
        const constructionIds = row.constructionId || this.ids;
        this.$confirm(
          '是否确认删除用户编号为"' + constructionIds + '"的数据项?',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return deleteInfo(constructionIds);
          })
          .then(() => {
            this.getList(this.supplierId,this.teamId);
            this.msgSuccess("删除成功");
          })
          .catch(function () {});
      },

      // 类别状态修改
      handlePublishStatusChange(row) {
        let text = row.publishStatus === "0" ? "上架" : "下架";
        this.$confirm(
          '确认要"' + text + '""' + row.constructionName + '"人员吗?',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return changeInfoStatus(row.constructionId, row.publishStatus);
          })
          .then(() => {
            this.msgSuccess(text + "成功");
          })
          .catch(function () {
            row.publishStatus = row.publishStatus === "1" ? "0" : "1";
          });
      },

      /*修改审核状态 **/
      handleAuditStatusChange(row){
        let text = row.auditStatus === "1" ? "通过" : "退审";
        this.$confirm(
          '确认要' + text + '"' + row.constructionName + '"人员的审批吗?',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return changeInfoAuditStatus(row.constructionId, row.auditStatus);
          })
          .then(() => {
            this.msgSuccess(text + "成功");
          })
          .catch(function () {
            row.auditStatus = row.auditStatus === "0" ? "1" : "0";
          });
      },

      /** 提交按钮 */
      submitForm: function() {
        console.log("***"+this.form.constructionType)
        this.form.constructionType = parseInt(this.$route.query.teamType);
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.constructionId != undefined) {
              updateInfo(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList(this.supplierId,this.teamId);
                }
              });
            } else {
              this.form.supplierId = this.supplierId;
              this.form.teamId = this.teamId;
              addInfo(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList(this.supplierId,this.teamId);
                }
              });
            }
          }
        });
      },

      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有施工人员信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      },
    }
  }
</script>

<style scoped>

</style>
